local folder "sum_stats"

capture drop tmp_samp
*gen tmp_samp = (main_sample==1) & (eca_ind<=2) & interp_flag==0 & WCport==1 // this one drops vessels we don't have characteristics for
* main sample drops tracks without chars, but we want those here
*gen tmp_samp = (bad_months==0) & (dist_time_outlier==0) &  (speed_outlier==0) & (eca_ind<=2) & (interp_flag==0) & (WCport==1) 
gen tmp_samp = (dist_time_outlier==0) &  (speed_outlier==0) & (eca_ind<=2) & (interp_flag==0) & (WCport==1) 
* dropping interpolated tracks to avoid double counting 
* don't drop bad months because we include these counts 

preserve

***** Vessel Characteristics ***************************
**** Only vessels going to CA ports
local file "`folder'/vessel_chars.tex"
local format "%9.3gc"

local vars length power_kw avg_speed tkm_cons // tdtonne_inm_pre_cons
local var_lables length "Length (m)" power_kw "Main Engine Power (kW)" avg_speed "Speed (km/h)" tkm_cons "Fuel Consumption (t/km)" // tdtonne_inm_pre_cons "Damage (\$/t)"

eststo clear
bysort vesseltype_regstr : eststo : estpost summarize `vars' if tmp_samp & eca_ind==0 & CAport==1

esttab  using "`file'" , replace /// 
	cell(mean (fmt(`format')) sd (par fmt(`format')) ) /// 
	booktabs unstack nogaps  frag	///  
	varlabels(`var_lables')  nonote  nonumber ///
	nodepvar collabels(none) nomtitle nolines   ///
	mgroups("Container" "Other Cargo" "Tanker", pattern(1  1 1) ) ///
	prehead(\begin{tabular}{lcccc} \toprule)  ///
	posthead(\midrule ) ///
	postfoot(\bottomrule \end{tabular})
	
***** Voyage Counts Table ******************************
local file "`folder'/vessel_counts_fullsample.tex"
local format "%9.0gc"

local ves_var vesseltype_regstr_filled

*tab vesseltype_regstr_filled if tmp_samp 
*tab vesseltype_regstr if tmp_samp 

eststo clear
eststo : estpost summarize tmp_samp porttoport enterexit if tmp_samp & `ves_var'~=""  
bysort `ves_var' : eststo : estpost summarize tmp_samp porttoport enterexit if tmp_samp
esttab  using "`file'" , replace ///
	cell(sum (fmt(`format'))) /// 
	booktabs unstack nogaps  frag	///  
	varlabels( tmp_samp "Total Voyages"  porttoport "\quad Port-to-Port" enterexit "\quad Entrance/Exit")  label noobs nonote  nonumber ///
	nodepvar collabels(none) nomtitle   ///
	mgroups("Total" "Container" "Other Cargo" "Tanker", pattern(1  1  1 1) ) ///
	prehead(\begin{tabular}{lcccc} \toprule) /// 
	posthead(\midrule \multicolumn{5}{l}{\textbf{(i) All West Coast Ports }}\\)
	
local socal_vars tmp_samp LALB SD HUE porttoport SFBay SEA POR enterexit	south_broad west north
local socal_var_labels tmp_samp "Total Voyages" LALB "\quad LA/LB"  SD "\quad San Diego" HUE "\quad Hueneme" /// 
					   porttoport "\quad Port-to-Port" SFBay "\quad \quad to/from San Francisco Bay" SEA "\quad \quad to/from Seattle" ///
					   POR "\quad \quad to/from Portland" enterexit "\quad Entrance/Exit" south_broad "\quad \quad South" west "\quad \quad West" north "\quad \quad North"
eststo clear
eststo : estpost summarize `socal_vars' if tmp_samp & port_agg=="SoCal" & `ves_var'~=""  
bysort `ves_var' : eststo : estpost summarize `socal_vars' if tmp_samp & port_agg=="SoCal"  
esttab using "`file'"  , append /// 
	cell(sum (fmt(`format'))) /// 
	booktabs unstack nogaps  frag	///  
	varlabels(`socal_var_labels')  noobs nonote  nonumber ///
	nodepvar collabels(none) nomtitle nolines   ///
	prehead(\\ \multicolumn{5}{l}{\textbf{(ii) Southern California Ports }}\\)
	
local nocal_vars tmp_samp SFBay porttoport SEA POR enterexit south_broad west north
local nocal_var_labels tmp_samp "Total Voyages" SFBay "\quad San Francisco Bay" /// 
					   porttoport "\quad Port-to-Port" SEA "\quad \quad to/from Seattle" POR "\quad \quad to/from Portland" ///
					   enterexit "\quad Entrance/Exit" south_broad "\quad \quad South" west "\quad \quad West" north "\quad \quad North"
eststo clear
eststo : estpost summarize `nocal_vars' if tmp_samp & port_agg=="NoCal"  & `ves_var'~=""  
bysort `ves_var' : eststo : estpost summarize `nocal_vars' if tmp_samp & port_agg=="NoCal"  
esttab  using "`file'" , append /// 
	cell(sum (fmt(`format'))) /// 
	booktabs unstack nogaps  frag	///  
	varlabels(`nocal_var_labels')  noobs nonote  nonumber ///
	nodepvar collabels(none) nomtitle nolines   ///
	prehead(\\ \multicolumn{5}{l}{\textbf{(iii) Northern California Ports }}\\) 

local otherWC_vars tmp_samp SEA POR porttoport enterexit south_broad west north	
local otherWC_var_labels tmp_samp "Total Voyages" SEA "\quad Seattle" POR "\quad Portland" ///
					   porttoport "\quad Port-to-Port" enterexit "\quad Entrance/Exit" south_broad "\quad \quad South" west "\quad \quad West" north "\quad \quad North"
					   
eststo clear
eststo : estpost summarize `otherWC_vars' if tmp_samp & port_agg=="OtherWC" & `ves_var'~="" 
bysort `ves_var' : eststo : estpost summarize `otherWC_vars' if tmp_samp & port_agg=="OtherWC"  
esttab  using "`file'" , append /// 
	cell(sum (fmt(`format'))) /// 
	booktabs unstack nogaps  frag	///  
	varlabels(`otherWC_var_labels')  noobs nonote  nonumber ///
	nodepvar collabels(none) nomtitle nolines   ///
	prehead(\\ \multicolumn{5}{l}{\textbf{(iv) Other West Coast Ports }}\\) ///
	postfoot(\bottomrule \end{tabular})

restore

	
* getting voyage counts and saving for final welfare
* counts by vessel type and route for period ECA is in place
preserve
	collapse (count) dist  if eca_ind==1 & tmp_samp==1 & vesseltype_regstr_filled~="" /// 
			, by(port_agg vesseltype_regstr_filled route_type )	
	rename dist tracks
	outsheet using "track_counts.csv" , comma	replace	
restore	

drop tmp_samp



